<?php
/**
 * Created by PhpStorm.
 * User: x
 * Date: 2018/9/15
 * Time: 17:00
 */

namespace app\index\controller;
use app\common\controller\HomeBase;
use app\api\controller\Elf as ElfApi;
use think\View;
use think\Request;
use think\Controller;
use app\common\model\Aiusers as AiuserModel;
use app\common\model\Zzzailogs as AilogsModel;
use app\common\model\Aimsgrules as AimsgrulesModel;
use app\common\model\Aimsgs as AimsgsModel;
use app\common\model\Aitalk as AitalkModel;
use app\common\model\Aiusersinfo as AiuserinfoModel;

class Elf extends ElfApi{
    private $_aiuser_model;
    private $_ailogs_model;
    private $_aimsgrules_model;
    private $_aimsgs_model;
    private $_aitalk_model;
    private $_aiuserinfo_model;
    public function _initialize()
    {
        /* * 用户表 * */
        $this->_aiuser_model = new AiuserModel();
        /* * 聊天话术 * */
        $this->_aimsgrules_model = new AimsgrulesModel();
        /* * 聊天记录 * */
        $this->_aimsgs_model = new AimsgsModel();
        /* * 聊天匹配key => value * */
        $this->_aitalk_model = new AitalkModel();
        /* * 用户画像记录表 * */
        $this->_aiuserinfo_model = new AiuserinfoModel();
        /* * 日志记录 * */
        $this->_ailogs_model = new AilogsModel();
    }

    public function index(){
        return $this->fetch();
    }

    // 用户信息采集

    public function talk(){
        $reply_msg = '';
        if (Request::instance()->isPost()) {
            $request = Request::instance()->post();
            dump($request);
            $request['typeid'] = 1;
            echo $request['id'];
//            //1 判断用户是否存在
            $user_info = self::has_user($request['id']);
            dump($user_info);
//            if($user_info){
//                // 老用户, 二次聊天
//            }else{
//                // 新用户注册
//                $new_user = self::create_user($request);
//                // 发送用户注册日志
//                $log_data = [
//                    'user_sign' => $request['id'], 'log' => '新用户注册'
//                ];
//                if($new_user) self::zzzlogs($log_data);
//                // 发送消息给客户
//                $reply_msg = ['typeid'=>$request['typeid'],'msgtype'=>0,'msgsign'=>'msgfirst'];
//            }


        }else{
//            echo '消息测试接口';
            $reply_msg = '消息测试接口';
        }
//        return json($reply_msg);
        return $this->fetch(
            'talk',[
                'msg'=> $reply_msg
            ]
        );
    }




}